<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :md="6" :sm="8">
            <a-form-item label="新闻标题">
              <a-input
                placeholder="请输入新闻标题"
                v-model="queryParam.categoryName"
              ></a-input>
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="8">
            <span
              style="float: left; overflow: hidden"
              class="table-page-search-submitButtons"
            >
              <a-button type="primary" @click="searchQuery" icon="search"
                >查询</a-button
              >
              <a-button
                type="primary"
                @click="searchReset"
                icon="reload"
                style="margin-left: 8px"
                >重置</a-button
              >
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>

    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-button @click="handleAdd" type="primary" icon="plus">新增</a-button>
    </div>
    <!-- table区域-begin -->
    <div>
      <div class="ant-alert ant-alert-info" style="margin-bottom: 16px">
        <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择
        <a style="font-weight: 600">{{ selectedRowKeys.length }}</a
        >项
        <a style="margin-left: 24px" @click="onClearSelected">清空</a>
        <CustomColList :propsDefColumns.sync="columns" @refresh="loadData" />
      </div>

      <a-table
        ref="table"
        size="middle"
        bordered
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        :rowSelection="{
          selectedRowKeys: selectedRowKeys,
          onChange: onSelectChange,
        }"
        @change="handleTableChange"
        :scroll="{ x: true }"
      >
        <template slot="avatarslot" slot-scope="text, record">
          <div class="anty-img-wrap">
            <a-avatar
              shape="square"
              :src="getAvatarView(record.cover)"
              icon="picture"
              style="backgroundcolor: #87d068"
            />
          </div>
        </template>

        <span slot="status" slot-scope="text, record">
          <a-switch
            checked-children="已发布"
            un-checked-children="未发布"
            :checked="text == 1 ? true : false"
            @change="changeStatus(...arguments, record.id)"
          />
        </span>
        <span slot="action" slot-scope="text, record">
          <a @click="handleEdit(record)">编辑</a>
          <a-divider type="vertical" />
          <a-popconfirm
            title="确定删除吗?"
            @confirm="() => handleDelete(record.id)"
          >
            <a>删除</a>
          </a-popconfirm>
        </span>
      </a-table>
    </div>
    <!-- table区域-end -->
    <NewsModal ref="modalForm" @ok="modalFormOk"></NewsModal>
  </a-card>
</template>

<script>
import { ListMixin } from "../../mixins/ListMixin";
import CustomColList from "@/components/custom/CustomColList";
import NewsModal from "./NewsModal";
import { postAction } from "@/api/basic";
import dayjs from "dayjs";
export default {
  name: "NewsList",
  mixins: [ListMixin],
  components: {
    CustomColList,
    NewsModal,
  },
  data() {
    return {
      description: "新闻列表",
      // 表头
      columns: [
        {
          title: "#",
          dataIndex: "",
          key: "rowIndex",
          width: 60,
          align: "center",
          customRender: function (t, r, index) {
            return parseInt(index) + 1;
          },
        },
        {
          title: "资讯标题",
          align: "center",
          dataIndex: "title",
        },
        {
          title: "封面",
          align: "center",
          dataIndex: "cover",
          scopedSlots: { customRender: "avatarslot" },
        },
        {
          title: "作者",
          align: "center",
          dataIndex: "auth",
        },
        {
          title: "创建时间",
          align: "center",
          dataIndex: "createdDate",
          customRender: function (t, r, index) {
            return dayjs(t).format("YYYY-MM-DD HH:mm:ss");
          },
        },
        {
          title: "内容",
          align: "center",
          width: 400,
          dataIndex: "content",
        },
        {
          title: "状态",
          align: "center",
          dataIndex: "status",
          fixed: "right",
          width: 100,
          scopedSlots: { customRender: "status" },
        },
        {
          title: "操作",
          dataIndex: "action",
          align: "center",
          width: 120,
          fixed: "right",
          scopedSlots: { customRender: "action" },
        },
      ],
      url: {
        list: "/news/list",
        delete: "/news/delete",
        edit: "/news/edit",
        changeStatus: "/news/changeStatus",
      },
    };
  },
  computed: {},
  methods: {
    getAvatarView: function (cover) {
      let url = "";
      if (cover) {
        url = JSON.parse(cover)[0].absolute_path;
      }
      return url;
    },
    changeStatus(val, e, id) {
      postAction(this.url.edit, {
        id: id,
        status: val ? 1 : 0,
      }).then((res) => {
        if (res.success) {
          this.$message.success("发布状态更改成功");
          this.loadData();
        } else {
          this.$message.error("发布状态更改失败," + res.message);
        }
      });
    },
  },
};
</script>
<style scoped></style>
